package com.imedway.ihe.dtz.xds.registry.sub;

import java.util.ArrayList;
import java.util.List;

import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLAssociation;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLClassification;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLExtrinsicObject;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.EbXMLRegistryPackage;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLRegistryResponse30;
import org.openehealth.ipf.commons.ihe.xds.core.ebxml.ebxml30.EbXMLSubmitObjectsRequest30;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.Transactional;

import com.imedway.ihe.dtz.xds.dao.Iti42Dao;
import com.imedway.ihe.dtz.xds.entity.XdsFileInfo;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Configuration
public class Iti42SubRegistry {
	
	@Autowired
	private Iti42Dao iti42Dao;
	
	@Transactional //小心的抓异常，不然影响事务
	public void run(EbXMLRegistryResponse30 ebXMLRegistryResponse30,EbXMLSubmitObjectsRequest30 ebXMLBody) {
	 
        log.info("----执行方法Iti42SubRegistry.run----");  
										
		List<EbXMLExtrinsicObject> ebXMLExtrinsicObjectList = ebXMLBody.getExtrinsicObjects();
		if (ebXMLExtrinsicObjectList != null && !ebXMLExtrinsicObjectList.isEmpty()) {
			iti42Dao.saveEbXMLExtrinsicObjectList(ebXMLExtrinsicObjectList);
		}

		List<EbXMLRegistryPackage> ebXMLRegistryPackageList = ebXMLBody.getRegistryPackages();
		if (ebXMLRegistryPackageList != null && !ebXMLRegistryPackageList.isEmpty()) {
			iti42Dao.saveEbXMLRegistryPackageList(ebXMLRegistryPackageList);
		}

		List<EbXMLAssociation> ebXMLAssociationList = ebXMLBody.getAssociations();
		if (ebXMLAssociationList != null && !ebXMLAssociationList.isEmpty()) {
			iti42Dao.saveEbXMLAssociationList(ebXMLAssociationList);
		}

		List<EbXMLClassification> ebXMLClassificationList = ebXMLBody.getClassifications();
		if (ebXMLClassificationList != null && !ebXMLClassificationList.isEmpty()) {
			iti42Dao.saveEbXMLClassificationList(ebXMLClassificationList, null);
		}

		List<XdsFileInfo> xdsFileInfoList = new ArrayList<XdsFileInfo>();
		XdsFileInfo xdsFileInfo = new XdsFileInfo();
		xdsFileInfoList.add(xdsFileInfo);
		iti42Dao.saveXdsFileInfo(xdsFileInfoList);
				
	}

	
	
}
